package com.chushouya.manager.dao.repository;

import com.chushouya.manager.dao.entity.FinanceAccountEntity;
import com.chushouya.manager.dao.mapper.FinanceAccountEntityMapper;
import com.chushouya.manager.dto.admin.finance.FinanceAccountQuery;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

@Repository
public class FinanceAccountRepository extends BaseRepository<FinanceAccountEntityMapper, FinanceAccountEntity, Long> {

    public FinanceAccountEntity selectOne(FinanceAccountQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    private Example buildExample(FinanceAccountQuery query) {
        Example example = buildExample();
        if (Objects.nonNull(query.getAccountUserId())) {
            example.and().andEqualTo("accountUserId", query.getAccountUserId());
        }
        if (Objects.nonNull(query.getAccountUserType())) {
            example.and().andEqualTo("accountUserType", query.getAccountUserType());
        }
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        if (Strings.isNotEmpty(query.getAccountUserName())) {
            example.and().andLike("accountUserName", Strings.format("%{}%", query.getAccountUserName()));
        }
        return example;
    }

    public PageResult<FinanceAccountEntity> selectPageResult(FinanceAccountQuery query) {
        return this.selectPage(query, this.buildExample(query));
    }
}
